package studentinformationmanagementsystem.service.impl;

import studentinformationmanagementsystem.dao.TeacherDao;
import studentinformationmanagementsystem.dao.impl.TeacherDaoImpl;
import studentinformationmanagementsystem.entity.Page;
import studentinformationmanagementsystem.entity.Student;
import studentinformationmanagementsystem.entity.Teacher;
import studentinformationmanagementsystem.service.TeacherService;

import java.util.List;

/**
 * @author 苗庆
 * @version 1.0
 */
public class TeacherServiceImpl implements TeacherService {

      TeacherDao  teacherDao=new TeacherDaoImpl();
    /**
     * 登陆
     *
     * @param teacher
     * @return
     */
    @Override
    public Teacher login(Teacher teacher) {
        return teacherDao.queryByUsernameAndPassword(teacher);
    }


    /**
     * 检查教师工号是否已存在
     *
     * @param teacher
     * @return true 教师工号已存在
     */
    @Override
    public boolean isExistsUsername(Teacher teacher) {

        boolean falg=false;

        Teacher teacher1 = teacherDao.queryByUsername(teacher);

        if (teacher1!=null){
            falg=true;
        }

        return falg;
    }

    /**
     * 分页查询
     * @param pageNo  查询页码
     * @param pageSize  每页的记录数
     * @return
     */
    @Override
    public Page<Teacher> page(int pageNo, int pageSize) {
        /**
         * 当前页码:pageNo
         * 总页码:pageTotal
         * 当前页显示的数量:pageSize
         * 总记录数:pageTotalCount
         * 当前页数据:items
         */

        //访问页面的记录条数的开始行数：当前页码-1*每页展示的记录行数
        int bigin=(pageNo-1)*pageSize;


        //总记录数量
        Integer pageTotalCount=teacherDao.queryForPageTotalCount();

        //总页码数量=总记录数/每页记录显示数量，然后判断是否可以被除尽，如果不能除尽，那么总的页码数量就需要+1
        Integer pageTotal=pageTotalCount/pageSize;

        if (pageTotalCount%pageSize!=0){
            pageTotal++;
        }


        //当前页码数据
        List<Teacher> items = teacherDao.queryForPageItems(bigin, pageSize);

        Page<Teacher> objectPage = new Page<Teacher>(pageNo,pageTotal,pageSize,pageTotalCount,items);

        return objectPage;
    }

    /**
     * 添加教师信息用户
     *
     * @param teacher
     */
    @Override
    public int register(Teacher teacher) {
        return teacherDao.insert(teacher);
    }

    /**
     *  根据ID更新教师信息
     * @param teacher  教师对象
     * @return 受影响的记录行数
     */
    @Override
    public int update(Teacher teacher) {
        return teacherDao.update(teacher);
    }

    /**
     *  根据ID删除教师
     * @param teacher 教师ID
     * @return 受影响的记录行数
     */
    @Override
    public int delete(Teacher teacher) {
        return teacherDao.delete(teacher);
    }

    /**
     * 查询所有教师信息
     * @return 所有教师信息
     */
    @Override
    public List<Teacher> queryAll() {
        return teacherDao.queryAll();
    }


    /**
     * 根据教师ID查询教师信息
     * @param teacher 教师ID
     * @return 教师信息
     */
    @Override
    public Teacher queryById(Teacher teacher) {
        return teacherDao.queryById(teacher);
    }

}
